package com.jzo2o.foundations.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzo2o.api.foundations.dto.response.ServeItemResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.dto.response.*;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private ServeMapper serveMapper;
    @Resource
    private IRegionService regionService;
    @Resource
    private IServeService serveService;
    @Resource
    private IServeItemService serveItemService;

    /**
     * @param regionId
     * @return
     */
    @Override
    @Caching(cacheable = {
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON , key = "#regionId" ,unless = "#result.size() != 0",cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON , key = "#regionId" ,unless = "#result.size() == 0",cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeCategoryResDTO> queryServeCategoryListByRegionId(Long regionId) {
        //校验当前城市服务是否为启用状态
        Region region = regionService.getById(regionId);
        if (region.getActiveStatus() == FoundationStatusEnum.DISABLE.getStatus() || ObjectUtil.isNull(region)) {
            return Collections.emptyList();
        }
        //根据区域id查询服务分类列表
        List<ServeCategoryResDTO> serveCategoryResDTOList= serveMapper.queryServeCategoryListByRegionId(regionId);
        if (ObjectUtil.isEmpty(serveCategoryResDTOList)) {
            return Collections.emptyList();
        }

        //服务类型取前两个，类型下的服务取前四个
        List<ServeCategoryResDTO> serveCategoryResDTO = new ArrayList<>(serveCategoryResDTOList.size() >= 2 ? serveCategoryResDTOList.subList(0,2):serveCategoryResDTOList.subList(0 ,serveCategoryResDTOList.size()));
        for (ServeCategoryResDTO serveCategoryResDTOItem : serveCategoryResDTOList){
            List<ServeSimpleResDTO> serveSimpleResDTOList = new ArrayList<>(serveCategoryResDTOItem.getServeResDTOList().size() >= 4 ? serveCategoryResDTOItem.getServeResDTOList().subList(0,4) : serveCategoryResDTOItem.getServeResDTOList().subList(0 ,serveCategoryResDTOItem.getServeResDTOList().size()));
            serveCategoryResDTOItem.setServeResDTOList(serveSimpleResDTOList);
        }

        return serveCategoryResDTO;
    }

    @Override
    @Caching(cacheable = {
            @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE , key = "#regionId" ,unless = "#result.size() != 0",cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE , key = "#regionId" ,unless = "#result.size() == 0",cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeAggregationTypeSimpleResDTO> queryServeTypeListByRegionId(Long regionId) {
        //校验当前城市服务是否为启用状态
        Region region = regionService.getById(regionId);
        if (region.getActiveStatus() == FoundationStatusEnum.DISABLE.getStatus() || ObjectUtil.isNull(region)) {
            return Collections.emptyList();
        }
        return serveMapper.queryServeTypeListByRegionId(regionId);
    }

    @Override
    @Caching(cacheable = {
            @Cacheable(value = RedisConstants.CacheName.HOT_SERVE , key = "#regionId" ,unless = "#result.size() != 0",cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            @Cacheable(value = RedisConstants.CacheName.HOT_SERVE , key = "#regionId" ,unless = "#result.size() == 0",cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeAggregationSimpleResDTO> queryHotServeList(Long regionId) {
        //校验当前城市服务是否为启用状态
        Region region = regionService.getById(regionId);
        if (region.getActiveStatus() == FoundationStatusEnum.DISABLE.getStatus() || ObjectUtil.isNull(region)) {
            return Collections.emptyList();
        }
        return serveMapper.queryHotServeList(regionId);
    }

    @Override
//    @Caching(cacheable = {
//            @Cacheable(value = RedisConstants.CacheName.SERVE_ITEM , key = "#result.getServeItemId()" ,cacheManager = RedisConstants.CacheManager.ONE_DAY),
//            @Cacheable(value = RedisConstants.CacheName.SERVE , key = "#id",cacheManager = RedisConstants.CacheManager.ONE_DAY)
//            @Cacheable(value = RedisConstants.CacheName.)
//    })
    public ServeAggregationSimpleResDTO findById(Long id) {
//        ServeAggregationSimpleResDTO serveAggregationSimpleResDTO = serveMapper.findById(id);
        Serve serve = serveService.queryServeByIdCache(id);

        ServeItemResDTO byId = serveItemService.queryServeItemAndTypeById(serve.getServeItemId());
        ServeAggregationSimpleResDTO serveAggregationSimpleResDTO = new ServeAggregationSimpleResDTO();

        serveAggregationSimpleResDTO.setId(serve.getId());
        serveAggregationSimpleResDTO.setServeItemId(serve.getServeItemId());
        serveAggregationSimpleResDTO.setServeItemName(byId.getName());
        serveAggregationSimpleResDTO.setServeItemImg(byId.getImg());
        serveAggregationSimpleResDTO.setUnit(byId.getUnit());
        serveAggregationSimpleResDTO.setPrice(serve.getPrice());
        serveAggregationSimpleResDTO.setDetailImg(byId.getDetailImg());
        return serveAggregationSimpleResDTO;
    }

}
